//
//  LeftMenuViewController.m
//  AloTruyen
//
//  Created by minhthong on 9/10/14.
//  Copyright (c) 2014 Khoai Nguyen Minh. All rights reserved.
//

#import "LeftMenuViewController.h"
#import "LeftMenuCell.h"
#import "HomeViewController.h"
#import "ITemScroller.h"
@interface LeftMenuViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong) NSMutableArray *contents;
@property (nonatomic, weak) IBOutlet UITableView *tableView;
@property (nonatomic,strong) BaseNavigationController *homeVC;
@property (nonatomic, weak) IBOutlet UIView *viewSachMoi;
@property (nonatomic, weak) IBOutlet UILabel *labelSachMoi;
@property (nonatomic, weak) IBOutlet UIScrollView *scroller;
@property (nonatomic,strong) UIView *viewSelected;
@end

@implementation LeftMenuViewController

#pragma mark - Initialize
-(NSMutableArray *)contents{
    if (!_contents) {
        _contents = [NSMutableArray array];
    }
    return _contents;
}
-(BaseNavigationController *)homeVC{
    if (!_homeVC) {
        _homeVC = [[BaseNavigationController alloc]initWithRootViewController:[[IBHelper sharedUIHelper]loadViewController:@"HomeVC" inStory:@"AloTruyen"]];
    }
    return _homeVC;
}
#pragma mark - Customize Subview
-(void)customizeSubview{
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = 40;
//    self.viewSachMoi.backgroundColor = UIColorFromRGB(0x0D0D1F);
    self.labelSachMoi.font = FontOpenSanWithSize(15);
    self.labelSachMoi.textColor = UIColorFromRGB(0xffffff);
    self.scroller.backgroundColor = UIColorFromRGB(0x0D0D1F);
}
-(void)customizeBar{
    @autoreleasepool {
        /* setup left menu BarButtonItem */
        self.leftButtonImageName = @"icon_home";
        self.titleLabelString = @"DANH MỤC";
    }
}
#pragma mark - Button Action
-(void)didTouchedOnLeftButton:(id)sender{
    BaseViewController* baseController =(BaseViewController*) self.navigationController.revealController.frontViewController;
    MainViewController* controller =(MainViewController*) baseController.parentViewController;
    BaseNavigationController * sdasd = (BaseNavigationController*)controller.aloTruyenNav;
    UIViewController* root = sdasd.topViewController;
    if (![root isKindOfClass:[HomeViewController class]]) {
        [sdasd popViewControllerAnimated:YES];
    }else{
        if ([sender isKindOfClass:[LeftMenu class]]) {
            NotifPost2Obj(@"kLeftMenu", sender);
        }
    }
    [self.navigationController.revealController showViewController:self.navigationController.revealController.frontViewController animated:YES completion:^(BOOL finished) {
        
    }];


}
#pragma mark - Helper Method
-(void)loadArchive{
    [appDelegate.dataCenter getMenuFromLeftMenu:@"/v1/left_menu.json"
                                       andBlock:^(NSArray* result, id error) {
                                           if (result) {
                                               if ([result count] > 0) {
                                                   [self.contents addObjectsFromArray:result];
                                                   [self.tableView reloadData];
                                               }
                                           }else if (error){
                                               [NLUtils showAlertWithMessage:error];
                                           }
                                       }];
}
#pragma mark - Notification
-(void)handleListEntity:(NSNotification*)notifi{
    int i = 0;
    for (NSDictionary *params in notifi.object) {
        ItemScroller *item = [[ItemScroller alloc]initItemWithParams:params andFrame:CGRectMake((i *35) + 3, 3, 60, 75)];
        [self.scroller addSubview:item];
        i++;
    }
    [self.scroller setContentSize:CGSizeMake((i*60)+(i*3), 90)];

}
#pragma mark - Init life vehicle
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
-(void)dealloc{
    NotifUnregAll(self);
}
- (void)viewDidLoad
{
    [self customizeBar];

    [super viewDidLoad];
    NotifReg(self, @selector(handleListEntity:), @"kListEntity");

    [self customizeSubview];
    [self loadArchive];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.contents.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier = @"LeftMenuCell";
    LeftMenuCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (!cell){
        tableView.backgroundColor =UIColorFromRGB(0x0D0D1F);
        cell = [[IBHelper sharedUIHelper]loadViewNib:[[LeftMenuCell class] description]];
        self.viewSelected = [[UIView alloc]initWithFrame:cell.frame];
        self.viewSelected.backgroundColor = UIColorFromRGB(0xE2433B);
        cell.selectedBackgroundView = self.viewSelected;
        cell.backgroundColor = UIColorFromRGB(0x0D0D1F);
    }
    
    [cell setLeftMenu:self.contents[indexPath.row]];

    return cell;

}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    /* disable check cell */
    for (int i = 0; i<self.contents.count; i++) {
        NSIndexPath *indexPathItem = [NSIndexPath indexPathForItem:i inSection:0];
        LeftMenuCell *cellToUnCheck = (LeftMenuCell*)[tableView cellForRowAtIndexPath:indexPathItem];
        cellToUnCheck.isCheck = NO;
        cellToUnCheck.iconCheck.image = [UIImage imageNamed:@"icon_uncheck"];
    }
    LeftMenuCell *cell = (LeftMenuCell*)[tableView cellForRowAtIndexPath:indexPath];
    if (!cell.isCheck) {
        cell.iconCheck.image = [UIImage imageNamed:@"icon_check"];
        cell.isCheck = YES;
        self.viewSelected.backgroundColor = UIColorFromRGB(0xE2433B);

    }else{
        cell.iconCheck.image = [UIImage imageNamed:@"icon_uncheck"];
        cell.isCheck = NO;
        self.viewSelected.backgroundColor = [UIColor clearColor];
    }
    cell.selectedBackgroundView = self.viewSelected;
    LeftMenu *object = self.contents[indexPath.row];
    [self didTouchedOnLeftButton:object];
}




#pragma mark - SKSTableViewDelegate

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
